package com.jiulan.hearthearing.ui.course.view;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseBottomDialog;
import com.jiulan.hearthearing.ui.course.activity.CourseDetailsFMActivity;
import com.jiulan.hearthearing.ui.course.adapter.CourseFMMuluDialogAdapter;
import com.jiulan.hearthearing.ui.course.model.CourseDetailsModel;

import java.util.List;

public class CoursePlayMuluSetDialog extends BaseBottomDialog {
    private Context context;
    private int chooseId;
    private boolean isPlaying;
    private List<CourseDetailsModel.DataBean.ClassListBean> data;
    private CourseFMMuluDialogAdapter muluDialogAdapter;

    private RecyclerView mRecyclerView;
    private TextView tvCancel;

    public CoursePlayMuluSetDialog(Context context, int chooseId, boolean isPlaying, List<CourseDetailsModel.DataBean.ClassListBean> data) {
        this.context = context;
        this.chooseId = chooseId;
        this.isPlaying = isPlaying;
        this.data = data;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_course_fm;
    }

    @Override
    public void bindView(View v) {
        mRecyclerView = v.findViewById(R.id.mRecyclerView);
        tvCancel = v.findViewById(R.id.tvCancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        muluDialogAdapter = new CourseFMMuluDialogAdapter(data);
        muluDialogAdapter.setClickPosition(chooseId, isPlaying);
        mRecyclerView.setAdapter(muluDialogAdapter);
        muluDialogAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.ivPlay:
                        if (chooseId == data.get(position).getId()) {
                            isPlaying = !isPlaying;
                            muluDialogAdapter.setClickPosition(chooseId, isPlaying);
                            muluDialogAdapter.notifyDataSetChanged();
                            sendBroadcast(5, position);
                        } else {
                            chooseId = data.get(position).getId();
                            isPlaying = true;
                            muluDialogAdapter.setClickPosition(chooseId, isPlaying);
                            muluDialogAdapter.notifyDataSetChanged();
                            sendBroadcast(6, position);
                        }
                        break;
                }
            }
        });
    }

    private void sendBroadcast(int action, int position) {
        Intent intent = new Intent(CourseDetailsFMActivity.LOCAL_BROADCAST);
        intent.putExtra("action", action);
        intent.putExtra("position", position);
        intent.putExtra("isPlaying", isPlaying);
        CourseDetailsFMActivity.localBroadcastManager.sendBroadcast(intent);
    }
}
